<?php

class Group extends Controller
{
	public function __construct()
	{
		parent::Controller();
		
		// Load libraries
		$this->load->database();

		// Load helpers
		$this->load->helper('url');
			
		// Load models
		$this->load->model('group_model', 'post');
	}
	
	public function index()
	{	
		// Get data from model
		$data['posts'] = $this->post->getAll();
		
		// Load views
		$this->load->view('header');
		
		$this->load->view('index', $data);
		$this->load->view('group', $data);
		$this->load->view('footer',$data);
	}
	
	public function read()
	{
		// Get id from uri
		$group_id = $this->uri->segment(3);
		
		// Get data from model
		$data['post'] = $this->post->getById($group_id);
		
		// Load views
		$this->load->view('header');
		$this->load->view('read', $data);
		$this->load->view('footer');
	}
	
	public function create()
	{	
		if($_POST)
		{
			// Build post object
			$post = new Group_model();
			$post->group_no = $this->input->post('group_no', TRUE);
			$post->value = $this->input->post('value', TRUE);
			$post->no_of_month = $this->input->post('no_of_month', TRUE);
			$post->no_of_members = $this->input->post('no_of_members', TRUE);
			$post->installment_amt = $this->input->post('installment_amt', TRUE);
			$post->auction_date = $this->input->post('auction_date', TRUE);
			$post->auction_time = $this->input->post('auction_time', TRUE);
			$post->max_bid = $this->input->post('max_bid', TRUE);
			$post->min_bid = $this->input->post('min_bid', TRUE);
			$post->due_fine = $this->input->post('due_fine', TRUE);
			$post->auction_type = $this->input->post('auction_type', TRUE);
			
			
			// Save post to database
			if ($post->save($post)) {
				redirect(base_url(), 'location');
			}
		}
	
		// Load helpers
		$this->load->helper('form');
	
		// Initialize form
		$data['action'] = site_url('group/create');
		$data['group_no'] = NULL;
		$data['value'] = NULL;
		$data['no_of_month'] = NULL;
		$data['no_of_members'] = NULL;
		$data['installment_amt'] = NULL;
		$data['auction_date'] = NULL;
		$data['auction_time'] = NULL;
		$data['max_bid'] = NULL;
		$data['min_bid'] = NULL;
		$data['due_fine'] = NULL;
		$data['auction_type'] = NULL;
		
		// Load views	
		//$this->load->view('header');
		$this->load->view('upsert', $data);
		
	}
		
	public function update()
	{
		if ($_POST) 
		{
			// Build post object
			$post = new Group_model();
			$post->group_id = $this->uri->segment(3);
			$post->group_no = $this->input->post('group_no', TRUE);
			$post->value = $this->input->post('value', TRUE);
			$post->no_of_month = $this->input->post('no_of_month', TRUE);
			$post->no_of_members = $this->input->post('no_of_members', TRUE);
			$post->installment_amt = $this->input->post('installment_amt', TRUE);
			$post->auction_date = $this->input->post('auction_date', TRUE);
			$post->auction_time = $this->input->post('auction_time', TRUE);
			$post->max_bid = $this->input->post('max_bid', TRUE);
			$post->min_bid = $this->input->post('min_bid', TRUE);
			$post->due_fine = $this->input->post('due_fine', TRUE);
			$post->auction_type = $this->input->post('auction_type', TRUE);
			
			// Save post to database
			if ($post->save($post)) {
				redirect(base_url(), 'location');
			}
		}
		
		// Get post from database
		$group_id = $this->uri->segment(3);
		$post = $this->post->getById($group_id);
		
		// Initialize form
		$this->load->helper('form');
		$data['action'] = site_url('group/update/'.$group_id);
		$data['group_no'] = $post->group_no;
		$data['value'] = $post->value;
		$data['no_of_month'] = $post->no_of_month;
		$data['no_of_members'] = $post->no_of_members;
		$data['installment_amt'] = $post->installment_amt;
		$data['auction_date'] = $post->auction_date;
		$data['auction_time'] = $post->auction_time;
		$data['max_bid'] = $post->max_bid;
		$data['min_bid'] = $post->min_bid;
		$data['due_fine'] = $post->due_fine;
		$data['auction_type'] = $post->auction_type;
		

		// Load views	
		
		$this->load->view('upsert', $data);
		
	}
	
	public function delete()
	{
		$post = new Group_model();
		$post->group_id = $this->uri->segment(3);
		if ($post->delete($post)) {
			redirect(base_url(), 'location');
		}
	}	
}